package com.notes.algo.exam;

import java.util.List;

/**
 * @author
 * @Describe 假设市场上每只股票不同天的价格会波动，但是当天价格恒定，如600570(恒生电子), 连续几天价格为50, 55, 60, 58, 如果我们在50买入，大于50卖出就可以获得盈利。
 * @date
 */
public class Solution3 {
    public static void main(String[] args) {
        java.util.Scanner sc = new java.util.Scanner(System.in);
        // 开始金额
        double beginCash = sc.nextDouble();
        // System.out.println(beginCash);

        // 价格天数
        int days = sc.nextInt();
        // System.out.println(days);

        // 历史价格
        String hisPricesStr = sc.next();
        // System.out.println(hisPricesStr);
        int[] hisPrices = new int[days];
        String[] priceArr = hisPricesStr.split(",");
        if (priceArr.length != days) {
            throw new java.lang.RuntimeException("输入长度错误！");
        }
        // 输入价格处理为int
        for (int i = 0; i < priceArr.length; i ++) {
            hisPrices[i] = Integer.valueOf(priceArr[i]);
        }
        // 获取最大的收益
        int maxProfile = getMaxProfit(beginCash, hisPrices);
        System.out.println(maxProfile);

        int maxProfile3 = getMaxProfit2(beginCash, hisPrices);
        System.out.println(maxProfile3);

        List<String> priceAscList = getPriceAsc(hisPrices);
        System.out.println(priceAscList); // 10000 10 1,2,3,4,3,5,4,8,9,1
    }

    /**
     * 贪心算法 不限交易次数
     * @param beginCash
     * @param prices
     * @return
     */
    public static int getMaxProfit(double beginCash, int[] prices) {
        int len = prices.length;
        // 特殊判断
        if (len < 2) {
            return 0;
        }
        // 最低购入价
        int buy = prices[0];
        int profit = 0;

        for (int i = 1; i < len; i++) {
            if (prices[i] < buy) {
                buy = prices[i];
            } else if (prices[i] > buy){
                double curProfit = (beginCash / buy) * (prices[i] - buy);
                profit += curProfit;
                beginCash += curProfit;
                // 更新购入最低价
                buy = prices[i];
            }
        }

        return profit;
    }

    public static int getMaxProfit2(double beginCash, int[] prices) {
        int length = prices.length;
        // 买入价格
        int buyPrice = prices[0];
        // 卖出价格
        int sellPrice = 0;
        // 收益
        int profile = 0;
        for (int i = 1; i < length; i ++) {
            if (prices[i] < buyPrice) {
                // 更低的价格作为买入价格
                buyPrice = prices[i];
            } else if (prices[i] > buyPrice) {
                String str = "" + buyPrice;
                // 后面的价格高于前面的价格
                while (i + 1 < length && prices[i + 1] > prices[i]) {
                    // 继续往后比较，直到价格比前一个价格小停止
                    str += "->" + prices[i];
                    i  ++;
                }

                str += "->" + prices[i];
                // 连续的价格上涨区间
                sellPrice = prices[i];
                // 计算区间的收益
                System.out.println("连续的价格上涨区间：" + str + "，本金为：" + beginCash +
                        "，买入价为：" + buyPrice +
                        "，可买股数为：" + beginCash + "/" + buyPrice + "=" + (beginCash / buyPrice) +
                        "，卖出价为：" + sellPrice +
                        "，价差为" + sellPrice  + "-" + buyPrice + "=" + (sellPrice - buyPrice) +
                        "，当前收益为：" + ((sellPrice - buyPrice) * (beginCash / buyPrice)));
                // 当前收益
                double curProfile = (sellPrice - buyPrice) * (beginCash / buyPrice);
                profile += curProfile;
                if (i + 1 != length) {
                    // 继续后续连续区间的查找
                    buyPrice = prices[i + 1];
                    // 开始资金变为原始资金+收益
                    beginCash += curProfile;
                    // 下一轮开始的价格位置
                    i = i + 1;
                }
            }
        }

        return profile;
    }

    public static java.util.List<String> getPriceAsc(int[] prices) {
        java.util.List<String> r = new java.util.ArrayList<String>();
        int length = prices.length;
        // 买入价格
        int buyPrice = prices[0];
        // 收益
        int profile = 0;
        for (int i = 1; i < length; i ++) {
            if (prices[i] < buyPrice) {
                // 更低的价格作为买入价格
                buyPrice = prices[i];
            } else if (prices[i] > buyPrice) {
                String str = "" + buyPrice;
                // 后面的价格高于前面的价格
                while (i + 1 < length && prices[i + 1] > prices[i]) {
                    // 继续往后比较，直到价格比前一个价格小停止
                    str += "->" + prices[i];
                    i  ++;
                }

                str += "->" + prices[i];
                // 计算区间的收益
                System.out.println("连续的价格上涨区间：" + str);
                r.add(str);
                if (i + 1 != length) {
                    // 继续后续连续区间的查找
                    buyPrice = prices[i + 1];
                    // 下一轮开始的价格位置
                    i = i + 1;
                }
            }
        }

        return r;
    }
}
